package com.cx.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Description
 * @Author 11597
 * @Date 2020/6/5 16:27
 */
public class Epidemic {

    public  String external() {
        String httpUrl = "http://api.tianapi.com/txapi/ncovabroad/index?key=eb2b54b1f7ae1ef1ebb5b1c3532edcf0";
        return request(httpUrl);
    }

    public  String domestic() {
        String httpUrl = "http://api.tianapi.com/txapi/ncov/index?key=eb2b54b1f7ae1ef1ebb5b1c3532edcf0";
        return request(httpUrl);
    }

    public  String province() {
        String httpUrl = "http://api.tianapi.com/txapi/ncovcity/index?key=eb2b54b1f7ae1ef1ebb5b1c3532edcf0";
        return request(httpUrl);
    }


    public static String request(String httpUrl) {
        System.out.println("接口获取数据");
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("POST");
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                System.out.println(sbf);
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


}
